#!/bin/sh /etc/rc.common
# Copyright (C) 2007 OpenWrt.org

START=99

start() {
	[ -f /etc/crontabs/root ] || touch /etc/crontabs/root
	vpncert_cron=`cat /etc/crontabs/root | grep '/etc/init.d/vpncert_clean restart'`
	if [ ! "${vpncert_cron}" ] ; then
		cat /etc/crontabs/root > /tmp/crontabs_vpncert
		echo "" >> /tmp/crontabs_vpncert
		echo '0 12 * * * /etc/init.d/vpncert_clean restart' >> /tmp/crontabs_vpncert
		cat /tmp/crontabs_vpncert | grep -v ^$ > /etc/crontabs/root
		/etc/init.d/cron restart
	fi
	config_load 'network'
	config_foreach start_instance 'interface'
}

start_instance() {
	config_get proto "$1" 'proto'
	if [ $proto = "openvpn" ] ; then
		ls /etc/openvpn | grep -v $1.key | grep -v $1.pem | grep -v $1-ta.key | grep -v $1-ca.pem | while read line
		do
			rm -f /etc/openvpn/$line
		done
	fi
	
	if [ $proto = "openconnect" ] ; then
		ls /etc/openconnect | grep -v ca-vpn-$1.pem | grep -v user-cert-vpn-$1.pem | grep -v user-key-vpn-$1.pem | while read line
		do
			rm -f /etc/openconnect/$line
		done
	fi
	
}


